package com.zhupf.springcloud.controller;

import com.zhupf.springcloud.entity.CommonResult;
import com.zhupf.springcloud.entity.Payment;
import com.zhupf.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("payment")
@Slf4j
public class PaymentController {
    @Autowired
    private PaymentService paymentService;

    @Value("${server.port}")
    private String port;

    @PostMapping(value = "/create")
    public CommonResult create(@RequestBody Payment payment){
        int result = paymentService.create(payment);

        if (result>0){
            return new CommonResult(200,"success,port"+port);
        }else {
            return new CommonResult(500,"error");
        }
    }

    @GetMapping(value = "/info/{id}")
    public CommonResult<Payment> info(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentById(id);
        int i = 10/0;
       return new CommonResult<Payment>(200,"success,port:"+port,payment);
    }
    @GetMapping(value = "/port")
    public String getPort(){
        return this.port;
    }

    @GetMapping("zipkin")
    public String zipkin(){
        return "O(∩_∩)O哈哈~，我是端口： "+port;
    }

    @GetMapping("/timeout")
    public String timeOut(){
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("timeout");
        return "timeout   : "+port;
    };
}
